package com.mindbook.vip.gawxchatinfo.config.shiro;

import com.mindbook.vip.gawxchatinfo.entity.user.UserFront;
import com.mindbook.vip.gawxchatinfo.service.user.UserFrontService;
import com.mindbook.vip.gawxchatinfo.utils.wx.WxUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author wxb
 * @Datatime 2022/4/6 13:16
 * @Descript
 */
public class UserFrontRealm extends AuthorizingRealm {

    @Autowired private UserFrontService userFrontService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("UserFrontRealm授权：doGetAuthorizationInfo");
        //前端访问不用进行授权的校验。
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("登录校验进入了前台认证的realm中");
        //1.将当前的token转换为特定的自定义loginTypeToken
        LoginTypeToken token = (LoginTypeToken) authenticationToken;

        //2.根据手机号查询用户是否存在
        UserFront userFront = userFrontService.selectByPhoneNumber(token.getUsername());
        if(userFront==null){
            throw new UnknownAccountException();
        }
        //3.构建AuthenticationInfo
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userFront,userFront.getOpenid(),"UserFrontRealm");
        return authenticationInfo;
    }
}
